package be.vdabantwerpen.gow.dao;

import java.util.ArrayList;
import java.util.List;

import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.exceptions.PostNotFoundException;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class UserDAOImpl implements UserDAO {
	private DatastoreService datastore = DatastoreServiceFactory
			.getDatastoreService();

	@Override
	public void create(GOWUser user) {
		Entity entity = toEntity(user);
		datastore.put(entity);
	}

	@Override
	public GOWUser read(int userId) {
		Key key = KeyFactory.createKey("User", userId);
		try {
			return toUser(datastore.get(key));
		} catch (EntityNotFoundException enfe) {
			return null;
		}
	}

	@Override
	public void update(GOWUser user) {
		Entity entity = toEntity(user);
		datastore.put(entity);
	}

	@Override
	public void delete(GOWUser user) {
		int userId = user.getUserId();
		Key key = KeyFactory.createKey("User", userId);
		datastore.delete(key);
	}

	@Override
	public List<GOWUser> findAll() {
		List<GOWUser> allUsers = new ArrayList<GOWUser>();
		Query query = new Query("User");
		List<Entity> allUserEntities = datastore.prepare(query).asList(
				FetchOptions.Builder.withDefaults());
		for (Entity entity : allUserEntities) {
			allUsers.add(toUser(entity));
		}
		return allUsers;
	}

	private Entity toEntity(GOWUser user) {
		Entity entity = new Entity("User", user.getUserId());
		entity.setProperty("firstName", user.getFirstName());
		entity.setProperty("lastName", user.getLastName());
		entity.setProperty("street", user.getStreet());
		entity.setProperty("number", user.getNumber());
		entity.setProperty("postalCode", user.getPostalCode());
		entity.setProperty("town", user.getTown());
		entity.setProperty("userName", user.getUserName());
		entity.setProperty("password", user.getPassword());
		entity.setProperty("role", user.getRole());
		entity.setProperty("userId", user.getUserId());
		return entity;
	}

	private GOWUser toUser(Entity entity) {
		String firstName = entity.getProperty("firstName").toString();
		String lastName = entity.getProperty("lastName").toString();
		String street = entity.getProperty("street").toString();
		int number = Integer.parseInt(entity.getProperty("number").toString());
		int postalCode = Integer.parseInt(entity.getProperty("postalCode")
				.toString());
		String town = entity.getProperty("town").toString();
		String password = entity.getProperty("password").toString();
		String userName = entity.getProperty("userName").toString();
		GOWUser user = new GOWUser(firstName, lastName, street, number, postalCode,
				town, userName, password);
		return user;
	}

}
